<?php


namespace SpecialOperations\GameBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use SpecialOperations\GameBundle\Entity\DistrictPosition;
use SpecialOperations\GameBundle\Entity\ProvincePosition;
use SpecialOperations\GameBundle\Entity\District;

class generatePositionsCommand extends ContainerAwareCommand
{
	/**
	 * Initialize whatever variables you may need to store beforehand, also load
         * Doctrine from the Container
	 */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        parent::initialize($input, $output); //initialize parent class method

        $this->em = $this->getContainer()->get('doctrine')->getEntityManager(); // This loads Doctrine, you can load your own services as well
    }
     /**
    * Configure the task with options and arguments
    */
    protected function configure()
    {
        parent::configure();

        $this

        ->setName('specops:generatePositions') // this is the command you would pass to console to run the command.
        ->setDescription('This command creates positions will be used in game');    // Your Description
      // Here are some parameter requirement constants:  InputOption::PARAMETER_OPTIONAL, InputOption::PARAMETER_REQUIRED

    }
     /**
    * Our console/task logic
    */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $provincesarray=array();
        $provi=0;
        for ($i = 0; $i < 5000; $i++) {            
            $prowincja = new ProvincePosition();

            do {
                
                $x=rand(-500, 500);
                $y=rand(-500, 500);
                $prowincja->setX($x);
                $prowincja->setY($y);
            } while (in_array($x.'--'.$y, $provincesarray));
            
            $provincesarray[]=$x.'--'.$y;
            $output->writeln('Creating province '.$provi);
            $this->em->persist($prowincja);
//            $this->em->flush();
            $dystrykty = rand(3, 8);
            $distarray=array();
            $disti=0;
            for ($j = 0; $j < $dystrykty; $j++) {
                $districtPosition = new DistrictPosition();
                $district = new District();
                $terrain=$this->em->getRepository('SpecialOperationsGameBundle:Terrain')->randomizeTerrain();
                $district->setTerrain($terrain);
                $output->writeln('District '.$disti.' terrain '.$terrain);
                
                $district->initializeResources();               
                $districtPosition->setProvince($prowincja);
                $district->setPosition($districtPosition);
                 $this->em->persist($district);
                do {
                    $x1=rand(-30, 30);
                    $y1=rand(-30, 30);
                    $districtPosition->setX($x1);
                    $districtPosition->setY($y1);
                } while (in_array($x1.'--'.$y1, $distarray));
                 $distarray[]=$x1.'--'.$y1;
                 $output->writeln('Creating district '.$disti.' in province '.$provi);
                $this->em->persist($districtPosition);
                $disti++;
            
                unset($districtPosition);
            }
            unset($prowincja,$distarray);
            $provi++;
        }   
        $this->em->flush();

//        $options = $input->getOptions(); // load options passted from console

        /* DO YOUR AWESOME STUFF HERE */

		 //get Doctrine EntityManager we initialized  in the initialize method and get a Repository to do something useful maybe
		# $this->em->getRepository('SplicedAwesomeBundle:AwesomeSauce')->getAllAwsomeSauce();

        // Display Some Output
       $output->writeln('Positions generated successfully!');

    }
}